package co.edu.usbcali.scrum.presentation.backingBeans;

import co.edu.usbcali.scrum.exceptions.*;
import co.edu.usbcali.scrum.modelo.*;
import co.edu.usbcali.scrum.modelo.dto.SeguimientoDTO;
import co.edu.usbcali.scrum.presentation.businessDelegate.BusinessDelegatorView;
import co.edu.usbcali.scrum.utilities.*;

import org.primefaces.component.calendar.*;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;

import org.primefaces.event.DateSelectEvent;
import org.primefaces.event.RowEditEvent;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TimeZone;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;


/**
 *
 * @author Zathura Code Generator http://code.google.com/p/zathura
 *
 */
public class SeguimientoView {
    private InputText txtCodigoAct_Actividades;
    private InputText txtCodigoDaiSpr_Dailysprint;
    private InputText txtCodigoDaiSpr;
    private InputText txtCodigoAct;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<SeguimientoDTO> data;
    private SeguimientoDTO selectedSeguimiento;

    public SeguimientoView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            SeguimientoDTO seguimientoDTO = (SeguimientoDTO) e.getObject();

            if (txtCodigoAct_Actividades == null) {
                txtCodigoAct_Actividades = new InputText();
            }

            txtCodigoAct_Actividades.setValue(seguimientoDTO.getCodigoAct_Actividades());

            if (txtCodigoDaiSpr_Dailysprint == null) {
                txtCodigoDaiSpr_Dailysprint = new InputText();
            }

            txtCodigoDaiSpr_Dailysprint.setValue(seguimientoDTO.getCodigoDaiSpr_Dailysprint());

            if (txtCodigoDaiSpr == null) {
                txtCodigoDaiSpr = new InputText();
            }

            txtCodigoDaiSpr.setValue(seguimientoDTO.getCodigoDaiSpr());

            if (txtCodigoAct == null) {
                txtCodigoAct = new InputText();
            }

            txtCodigoAct.setValue(seguimientoDTO.getCodigoAct());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtCodigoAct_Actividades != null) {
            txtCodigoAct_Actividades.setValue(null);
            txtCodigoAct_Actividades.setDisabled(true);
        }

        if (txtCodigoDaiSpr_Dailysprint != null) {
            txtCodigoDaiSpr_Dailysprint.setValue(null);
            txtCodigoDaiSpr_Dailysprint.setDisabled(true);
        }

        if (txtCodigoDaiSpr != null) {
            txtCodigoDaiSpr.setValue(null);
            txtCodigoDaiSpr.setDisabled(false);
        }

        if (txtCodigoAct != null) {
            txtCodigoAct.setValue(null);
            txtCodigoAct.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Seguimiento entity = null;

        try {
            SeguimientoId id = new SeguimientoId();
            id.setCodigoDaiSpr((((txtCodigoDaiSpr.getValue()) == null) ||
                (txtCodigoDaiSpr.getValue()).equals("")) ? null
                                                         : new String(
                    txtCodigoDaiSpr.getValue().toString()));
            id.setCodigoAct((((txtCodigoAct.getValue()) == null) ||
                (txtCodigoAct.getValue()).equals("")) ? null
                                                      : new String(
                    txtCodigoAct.getValue().toString()));
            entity = BusinessDelegatorView.getSeguimiento(id);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtCodigoAct_Actividades.setDisabled(false);
            txtCodigoDaiSpr_Dailysprint.setDisabled(false);
            txtCodigoDaiSpr.setDisabled(false);
            txtCodigoAct.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtCodigoAct_Actividades.setValue(entity.getActividades()
                                                    .getCodigoAct());
            txtCodigoAct_Actividades.setDisabled(false);
            txtCodigoDaiSpr_Dailysprint.setValue(entity.getDailysprint()
                                                       .getCodigoDaiSpr());
            txtCodigoDaiSpr_Dailysprint.setDisabled(false);
            txtCodigoDaiSpr.setValue(entity.getId().getCodigoDaiSpr());
            txtCodigoDaiSpr.setDisabled(true);
            txtCodigoAct.setValue(entity.getId().getCodigoAct());
            txtCodigoAct.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveSeguimiento(FacesUtils.checkString(
                    txtCodigoDaiSpr), FacesUtils.checkString(txtCodigoAct),
                FacesUtils.checkString(txtCodigoAct_Actividades),
                FacesUtils.checkString(txtCodigoDaiSpr_Dailysprint));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteSeguimiento(FacesUtils.checkString(
                    txtCodigoDaiSpr), FacesUtils.checkString(txtCodigoAct));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateSeguimiento(FacesUtils.checkString(
                    txtCodigoDaiSpr), FacesUtils.checkString(txtCodigoAct),
                FacesUtils.checkString(txtCodigoAct_Actividades),
                FacesUtils.checkString(txtCodigoDaiSpr_Dailysprint));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtCodigoDaiSpr == null) {
                txtCodigoDaiSpr = new InputText();
            }

            txtCodigoDaiSpr.setValue(selectedSeguimiento.getCodigoDaiSpr());

            if (txtCodigoAct == null) {
                txtCodigoAct = new InputText();
            }

            txtCodigoAct.setValue(selectedSeguimiento.getCodigoAct());

            BusinessDelegatorView.deleteSeguimiento(FacesUtils.checkString(
                    txtCodigoDaiSpr), FacesUtils.checkString(txtCodigoAct));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedSeguimiento);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String codigoDaiSpr, String codigoAct,
        String codigoAct_Actividades, String codigoDaiSpr_Dailysprint)
        throws Exception {
        try {
            BusinessDelegatorView.updateSeguimiento(codigoDaiSpr, codigoAct,
                codigoAct_Actividades, codigoDaiSpr_Dailysprint);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("SeguimientoView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtCodigoAct_Actividades() {
        return txtCodigoAct_Actividades;
    }

    public void setTxtCodigoAct_Actividades(InputText txtCodigoAct_Actividades) {
        this.txtCodigoAct_Actividades = txtCodigoAct_Actividades;
    }

    public InputText getTxtCodigoDaiSpr_Dailysprint() {
        return txtCodigoDaiSpr_Dailysprint;
    }

    public void setTxtCodigoDaiSpr_Dailysprint(
        InputText txtCodigoDaiSpr_Dailysprint) {
        this.txtCodigoDaiSpr_Dailysprint = txtCodigoDaiSpr_Dailysprint;
    }

    public InputText getTxtCodigoDaiSpr() {
        return txtCodigoDaiSpr;
    }

    public void setTxtCodigoDaiSpr(InputText txtCodigoDaiSpr) {
        this.txtCodigoDaiSpr = txtCodigoDaiSpr;
    }

    public InputText getTxtCodigoAct() {
        return txtCodigoAct;
    }

    public void setTxtCodigoAct(InputText txtCodigoAct) {
        this.txtCodigoAct = txtCodigoAct;
    }

    public List<SeguimientoDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataSeguimiento();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<SeguimientoDTO> seguimientoDTO) {
        this.data = seguimientoDTO;
    }

    public SeguimientoDTO getSelectedSeguimiento() {
        return selectedSeguimiento;
    }

    public void setSelectedSeguimiento(SeguimientoDTO seguimiento) {
        this.selectedSeguimiento = seguimiento;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
